{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# Example: Static inverse free-boundary equilibrium calculations (in DIII-D)\n", "\n", "---\n", "\n", "Here we generate a DIII-D equilibrium using the machine geometry and some discharge data in the [FUSE.jl repository](https://github.com/ProjectTorreyPines/FUSE.jl).\n", "\n", "The equilibrium/profile parameters are a mix of data from the repository and **completely made up**; please experiment on your own and change them to more realistic values as you please!" ] }, { "cell_type": "markdown", "id": "1", "metadata": {}, "source": [ "### Import packages" ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "import numpy as np" ] }, { "cell_type": "markdown", "id": "3", "metadata": {}, "source": [ "### Create the machine object" ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": {}, "outputs": [], "source": [ "# build machine\n", "\n", "from freegsnke import build_machine\n", "\n", "tokamak = build_machine.tokamak(\n", " active_coils_path=\"../machine_configs/DIII-D/DIII-D_active_coils.pickle\",\n", " passive_coils_path=\"../machine_configs/DIII-D/DIII-D_passive_coils.pickle\",\n", " limiter_path=\"../machine_configs/DIII-D/DIII-D_limiter.pickle\",\n", " wall_path=\"../machine_configs/DIII-D/DIII-D_wall.pickle\",\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "5", "metadata": {}, "outputs": [], "source": [ "# plot the machine\n", "\n", "fig, ax = plt.subplots(figsize=(8, 10), dpi=80)\n", "\n", "tokamak.plot(axis=ax, show=False) # plots the active coils and passive structures\n", "ax.plot(tokamak.limiter.R, tokamak.limiter.Z, \"k-\", linewidth=1.2) # plots the limiter\n", "\n", "ax.grid(alpha=0.5)\n", "ax.set_aspect(\"equal\")\n", "ax.set_xlim(0.55, 2.85)\n", "ax.set_ylim(-2.15, 2.15)\n", "ax.set_xlabel(r\"Major radius, $R$ [m]\")\n", "ax.set_ylabel(r\"Height, $Z$ [m]\")" ] }, { "cell_type": "markdown", "id": "6", "metadata": {}, "source": [ "### Instantiate an equilibrium" ] }, { "cell_type": "code", "execution_count": null, "id": "7", "metadata": {}, "outputs": [], "source": [ "from freegsnke import equilibrium_update\n", "\n", "eq = equilibrium_update.Equilibrium(\n", " tokamak=tokamak, # provide tokamak object\n", " Rmin=0.95, Rmax=2.43, # radial range\n", " Zmin=-1.42, Zmax=1.42,# vertical range\n", " nx=129, # number of radial grid points (needs to be of the form 2**n + 1)\n", " ny=129, # number of vertical grid points (needs to be of the form 2**n + 1)\n", ")" ] }, { "cell_type": "markdown", "id": "8", "metadata": {}, "source": [ "### Instantiate a profile object" ] }, { "cell_type": "code", "execution_count": null, "id": "9", "metadata": {}, "outputs": [], "source": [ "# equilibrium profiles from the FUSE DIII-D example\n", "psi_n = np.array([0.0, 0.015624999999999951, 0.031249999999999903, 0.04687500000000016, 0.062499999999999806, 0.07812500000000007, 0.09375000000000001, 0.10937499999999997, 0.12499999999999992, 0.1406250000000002, 0.15624999999999983, 0.17187500000000008, 0.18750000000000003, 0.203125, 0.21874999999999994, 0.2343749999999999, 0.24999999999999983, 0.2656250000000001, 0.28125000000000006, 0.296875, 0.31249999999999994, 0.3281249999999999, 0.3437499999999999, 0.3593750000000001, 0.3749999999999998, 0.39062500000000006, 0.40625, 0.42187499999999994, 0.4374999999999999, 0.45312500000000017, 0.4687499999999998, 0.48437500000000006, 0.5, 0.515625, 0.5312499999999999, 0.546875, 0.5625000000000001, 0.5781250000000001, 0.5937499999999999, 0.609375, 0.6250000000000001, 0.640625, 0.6562499999999998, 0.6718750000000001, 0.6875000000000002, 0.703125, 0.7187499999999999, 0.734375, 0.7500000000000001, 0.765625, 0.7812499999999999, 0.796875, 0.8125000000000001, 0.8281250000000001, 0.84375, 0.8593749999999998, 0.8750000000000001, 0.890625, 0.90625, 0.9218750000000001, 0.9375, 0.953125, 0.9687499999999999, 0.984375, 1.0])\n", "pprime_data = np.array([-52858.0359742854, -52134.66720863615, -51411.2984429869, -50687.929518182704, -49964.56075253345, -49241.1919868842, -48517.82306208001, -47794.45429643076, -47071.085530781515, -46347.71676513226, -45624.34784032807, -44900.97907467882, -44177.61030902957, -43454.241543380325, -42730.872618576126, -42007.50385292687, -41284.13508727763, -40560.76616247343, -39837.397396824184, -39114.02863117494, -38390.65986552568, -37667.29094072149, -36943.92217507224, -36220.55340942299, -35497.184484618796, -34773.81571896955, -34050.4469533203, -33327.07818767105, -32603.709262866854, -31880.340497217607, -31156.971731568356, -30433.602965919108, -29710.234041114913, -28986.86527546566, -28263.496509816414, -27540.127585012222, -26816.75881936297, -26093.390053713723, -25370.021288064472, -24646.652363260277, -23923.28359761103, -23199.914831961778, -22476.545907157586, -21753.177141508335, -21029.808375859084, -20306.439610209836, -19583.070685405644, -18859.701919756393, -18136.333154107142, -17412.96438845789, -16689.5954636537, -15966.22669800445, -15242.857884608718, -14519.489071212985, -13796.120273732746, -13072.751460337013, -12349.382646941282, -11626.013833545549, -10902.645020149816, -10179.276222669576, -9455.907409273845, -8732.53859587811, -8009.169782482379, -7285.80098500214, -6562.432171606408])\n", "ffprime_data = np.array([-0.21224472378304216, -0.20940737311957705, -0.2065648196810223, -0.20371706187582844, -0.20086410129554494, -0.19800593794017174, -0.19514257180970887, -0.1922740029041563, -0.18940022963196465, -0.18652125358468333, -0.18363707635386176, -0.1807476947564011, -0.1778531087923013, -0.1749533216446613, -0.17204833172193157, -0.16913813743256279, -0.1662227403681043, -0.16330214052855616, -0.1603763379139183, -0.15744533156926116, -0.15450912244951434, -0.1515677103955229, -0.14862109524813188, -0.14566927716649625, -0.14271225599146106, -0.13975003172302633, -0.136782604361192, -0.13380997406511308, -0.1308321406756346, -0.12784910435191152, -0.1248608647756339, -0.12186742226511167, -0.11886877682034483, -0.1158649281230235, -0.11285587649145752, -0.10984162176649201, -0.10682216410728186, -0.10379750335467217, -0.10076763950866291, -0.0977325725692541, -0.09469230269560067, -0.09164682972854767, -0.08859615382725004, -0.08554027467339793, -0.08247919258530122, -0.07941290756295986, -0.07634141928806401, -0.07326472807892354, -0.0701828337763835, -0.06709573653959885, -0.06400343620941465, -0.06090593278583088, -0.05780322626884754, -0.054695316817619605, -0.051582204272992094, -0.048463888634965036, -0.04534037006269335, -0.04221164839702211, -0.0390777236379513, -0.03593859594463588, -0.032794265157920895, -0.029644731277806355, -0.026489994463447193, -0.02333005439653353, -0.020164911554530197])\n", "\n", "from freegsnke.jtor_update import GeneralPprimeFFprime\n", "\n", "profiles = GeneralPprimeFFprime(\n", " eq=eq, # equilibrium object\n", " Ip=1083742, # plasma current [A]\n", " fvac=2.89, # vacuum R * B_tor [T m]\n", " psi_n=psi_n, # normalised poloidal flux\n", " pprime_data=pprime_data, # pressure derivative profile\n", " ffprime_data=ffprime_data, # FF' profile\n", " Ip_logic=True,\n", ")" ] }, { "cell_type": "markdown", "id": "10", "metadata": {}, "source": [ "### Load the static nonlinear solver" ] }, { "cell_type": "code", "execution_count": null, "id": "11", "metadata": {}, "outputs": [], "source": [ "from freegsnke import GSstaticsolver\n", "\n", "GSStaticSolver = GSstaticsolver.NKGSsolver(eq, seed=42)" ] }, { "cell_type": "markdown", "id": "12", "metadata": {}, "source": [ "### Constraints" ] }, { "cell_type": "code", "execution_count": null, "id": "13", "metadata": {}, "outputs": [], "source": [ "# boundary and null constraints\n", "Rx_upper, Zx_upper = 1.29, 1.12\n", "Rx_lower, Zx_lower = 1.27, -1.17\n", "\n", "null_points = [\n", " [Rx_upper, Rx_lower],\n", " [Zx_upper, Zx_lower],\n", "]\n", "\n", "isoflux_set = np.array([[[1.10, 2.23], [0.0, 0.0]]])\n", "\n", "from freegsnke.inverse import Inverse_optimizer\n", "\n", "constrain = Inverse_optimizer(\n", " null_points=null_points,\n", " isoflux_set=isoflux_set,\n", ")" ] }, { "cell_type": "markdown", "id": "14", "metadata": {}, "source": [ "### The inverse solve" ] }, { "cell_type": "code", "execution_count": null, "id": "15", "metadata": {}, "outputs": [], "source": [ "GSStaticSolver.inverse_solve(\n", " eq=eq,\n", " profiles=profiles,\n", " constrain=constrain,\n", " target_relative_tolerance=5e-4,\n", " target_relative_psit_update=2e-3,\n", " max_solving_iterations=50,\n", " l2_reg=np.array([1e-14] * 24),\n", " verbose=True,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "16", "metadata": {}, "outputs": [], "source": [ "# refine with a forward solve (now the currents are known)\n", "GSStaticSolver.solve(eq=eq, \n", " profiles=profiles, \n", " constrain=None, \n", " target_relative_tolerance=1e-9,\n", " verbose=False, # suppress output\n", " )" ] }, { "cell_type": "code", "execution_count": null, "id": "17", "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(figsize=(8, 10), dpi=80)\n", "\n", "ax.grid(zorder=0, alpha=0.75)\n", "ax.set_aspect(\"equal\")\n", "eq.tokamak.plot(axis=ax, show=False) # plots the active coils and passive structures\n", "eq.plot(axis=ax, show=False) # plots the equilibrium\n", "constrain.plot(axis=ax, show=False) # plots the constraints\n", "ax.plot(tokamak.limiter.R, tokamak.limiter.Z, \"k-\", linewidth=1.2) # plots the limiter\n", "ax.set_xlabel(r\"Major radius, $R$ [m]\")\n", "ax.set_ylabel(r\"Height, $Z$ [m]\")" ] }, { "cell_type": "code", "execution_count": null, "id": "18", "metadata": {}, "outputs": [], "source": [ "eq.tokamak.getCurrents()" ] }, { "cell_type": "code", "execution_count": null, "id": "19", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "freegsnke", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.20" } }, "nbformat": 4, "nbformat_minor": 5 }